package br.com.fogas.medicao.activity.helper;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.database.entidade.NotaFiscal;
import br.com.fogas.impressora.ImpressaoNotaEngine;
import br.com.fogas.impressora.ImpressoraMPTException;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.activity.PlanoLocalFragment;
import br.com.fogas.medicao.application.ApplicationFogas;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.medicao.application.LoadFragment;
import br.com.fogas.medicao.business.CancelarNotaBusiness;
import br.com.fogas.medicao.utils.MedicaoUtils;
import br.com.fogas.utils.FogasUtility;
import br.com.fogas.utils.StringUtility;
import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import br.com.fogas.utils.task.ServiceTask;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ImpressaoNotaHelper {

	private Fragment fragment;
	private View view;
	private TextView panelHeaderTitulo;
	private TextView local;
	private TextView cliente;
	private TextView condPagto;
	private TextView totalNota;
	private NotaFiscal notaImpressao;
	private Button btnImprimirNota, btnFaturarOutro, btnCancelarNota;
	private ImpressaoNotaEngine notaEngine;
	private FogasDatabase database;
	
	public ImpressaoNotaHelper(Fragment fragment, View view, NotaFiscal notaImpressao) {
		this.database = new FogasDatabase(fragment.getActivity());
		this.fragment = fragment;
		this.view = view;
		this.notaImpressao = notaImpressao;
		this.panelHeaderTitulo = (TextView) view.findViewById(R.id.headerDadosNotaTitleLabel); 
		this.local = (TextView) view.findViewById(R.id.local);
		this.cliente = (TextView) view.findViewById(R.id.cliente);
		this.condPagto = (TextView) view.findViewById(R.id.condPagto);
		this.totalNota = (TextView) view.findViewById(R.id.totalNota);
		
		this.btnImprimirNota = (Button) view.findViewById(R.id.btnImprimirNota);
		this.btnImprimirNota.setOnClickListener(imprimir);
		
		this.btnFaturarOutro = (Button) view.findViewById(R.id.btnFaturarOutro);
		this.btnFaturarOutro.setOnClickListener(faturarOutro);
		
		this.btnCancelarNota = (Button) view.findViewById(R.id.btnCancelarNota);
		this.btnCancelarNota.setOnClickListener(cancelarNota);
		
		try {
			this.notaEngine = new ImpressaoNotaEngine(fragment);
		} catch (ImpressoraMPTException e) {
			e.printStackTrace();
		}
		this.atualizarDadosTela();
	}
	
	private final OnClickListener imprimir = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			notaEngine.imprimir_1_2_ViaNota_Boleto(notaImpressao, 
					ApplicationFogas.getInstance().getConfiguracao());
		}
	};
	
	private final OnClickListener faturarOutro = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			LoadFragment load = new LoadFragment(fragment.getActivity().getSupportFragmentManager());
			load.initializeFragment(true, new PlanoLocalFragment(), Constants.FRAGMENT_PLANO_LOCAL);
		}
	};
	
	private final OnClickListener cancelarNota = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			cancelarNota();
		}
	};
	
	private void cancelarNota() {
		CancelarNotaBusiness business = new CancelarNotaBusiness(fragment, database, notaImpressao);
		
		ProgressDialogBuilder dialog = new ProgressDialogBuilder(fragment.getActivity())
		.setMessage(fragment.getString(R.string.dialog_cancelar_nota));
		
		ServiceTask task = new ServiceTask(dialog, business);
		task.execute();
	}
	
	private void atualizarDadosTela() {
		String nfe = "NFe "+StringUtility.removeZeroEsquerdaString(notaImpressao.getTx_nfnum());
		String serie = " Serie "+StringUtility.removeZeroEsquerdaString(notaImpressao.getTx_serie());
		this.panelHeaderTitulo.setText(nfe+" "+serie);
		
		this.local.setText(MedicaoUtils.nomeCliente(notaImpressao.getTx_plano_visitas().getLocal()));
		
		this.cliente.setText(MedicaoUtils.nomeCliente(notaImpressao.getTx_plano_visitas().getCliente()));
		
		String condPgto = new StringBuilder()
		.append(notaImpressao.getTx_cond_pagto().getTx_cod())
		.append(" - ")
		.append(notaImpressao.getTx_cond_pagto().getTx_desc())
		.toString();
		this.condPagto.setText(condPgto);
		
		String totalNota = FogasUtility.string.currencyFormat(notaImpressao.getVl_valor_nota().doubleValue());
		this.totalNota.setText(totalNota);
	}

	public Fragment getFragment() {
		return fragment;
	}

	public View getView() {
		return view;
	}

	public TextView getPanelHeaderTitulo() {
		return panelHeaderTitulo;
	}

	public TextView getLocal() {
		return local;
	}

	public TextView getCliente() {
		return cliente;
	}

	public TextView getCondPagto() {
		return condPagto;
	}

	public TextView getTotalNota() {
		return totalNota;
	}

	public NotaFiscal getNotaImpressao() {
		return notaImpressao;
	}
	
	public Button getBtnImprimirNota() {
		return btnImprimirNota;
	}
	
	public ImpressaoNotaEngine getNotaEngine() {
		return notaEngine;
	}
}